package com.cgy.controller;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.ResStatistics;
import com.cgy.entity.ZiYuan;
import com.cgy.repository.ResStatisricsRepository;
import com.cgy.service.ResStatisticsService;
import com.cgy.service.ZiYuanService;

import util.LayuiJsonUtils;

@RestController
@RequestMapping("resStatistics")
@CrossOrigin
public class ResStatisticsController {
	@Autowired
	private ResStatisticsService resService;
	
	@Autowired
	private ZiYuanService ziYuanService;
	@Autowired
	private ResStatisricsRepository  resStatisticsRepository;
	
	@GetMapping
	public LayuiJsonUtils<ResStatistics> findAllResStatisticsByrname(LayuiJsonUtils<ResStatistics> lj,ResStatistics r){
		Pageable pageable = PageRequest.of(lj.getPage(), lj.getLimit());
		Page<ResStatistics> resStatistics = resService.findBynameResStatistics(r, pageable);
		lj.setData(resStatistics.getContent());
		lj.setCount(resStatistics.getTotalElements());
		return lj;
	}
	
	//添加
	@PostMapping
	public ResStatistics addresStatistics(ResStatistics r,Integer zid) {
		ZiYuan ziyuan = new ZiYuan();
		ziyuan.setZid(zid);
		r.setZiYuan(ziyuan);
		return resService.addAndUpdateResStatistics(r);
	}
	
	//修改
	@PutMapping("upcishu")
	public ResStatistics updateresService(ResStatistics r,Integer zid) {
		ZiYuan z = new ZiYuan();
		z.setZid(zid);
		r.setZiYuan(z); 
		return resService.addAndUpdateResStatistics(r);
	} 
	
	//删除
	@DeleteMapping("{rid}")
	public Integer deleteresService(@PathVariable("rid") Integer rid) {
		return resService.deleteResStatistics(rid);
	}
	
	//根据月份和资源室id修改访问次数
	@RequestMapping("upCishu")
	public String upcishu(Integer zid) {
		System.out.println("我进入了修改次数的方法");
		Calendar now= Calendar.getInstance();
		Integer cs = resService.upCiShu(zid, now.get(Calendar.MONTH)+1+"月");
		return null;
	}
	
	
}
